﻿using SmartPlant.Core;
using SmartPlant.Model.UI;
using SmartPlant.Modules.Base.ViewModels;
using System.Windows;
using System.Windows.Controls.Primitives;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Docking;

namespace SmartPlant.Modules.Base.Views
{
    /// <summary>
    /// DomainExplorer
    /// </summary>
    public partial class DomainExplorer : RadPane, IPaneModel
    {
        public DomainExplorer()
        {
            InitializeComponent();
        }

        public DockState Position => DockState.DockedLeft;

        private void xTreeView_Checked(object sender, RoutedEventArgs e)
        {
            var x = (RadTreeViewItem)(e.OriginalSource as ToggleButton).GetParents().FirstOrDefault(p => p.GetType().Name == "RadTreeViewItem");
            x.IsSelected = true;
        }
        /// <summary>
        /// 打开右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            //关闭菜单
            //return;
            RadTreeViewItem clickedItemContainer = radContextMenu.GetClickedElement<RadTreeViewItem>();//右键时，对应的节点
            if ( clickedItemContainer == null)
            {
                return;
            }
            clickedItemContainer.IsSelected = true;
            if (clickedItemContainer != null)
            {
                var clickedItem = clickedItemContainer.Item as TreeNodeModel;

                if (clickedItem != null)
                {
                    if (clickedItem.NodeName != "空")
                    {
                        (this.DataContext as DomainExplorerViewModel).PreapreContextMenu(clickedItem);
                    }

                }
            }
            else
            {
                (this.DataContext as DomainExplorerViewModel).PreapreContextMenu(null);
            }
        }
    }
}